import type { ThemedToken } from "shiki/core";

export function uuid(): string {
  return (
    Math.random().toString(36).substring(2, 15) +
    Math.random().toString(36).substring(2, 15)
  );
}

export function getReactStyleFromToken(
  token: ThemedToken,
): Record<string, string> {
  if (token.htmlStyle) {
    const reactStyle: Record<string, string> = {};
    for (const [key, value] of Object.entries(token.htmlStyle)) {
      switch (key) {
        case "font-style":
          reactStyle.fontStyle = value;
          break;
        case "font-weight":
          reactStyle.fontWeight = value;
          break;
        case "background-color":
          reactStyle.backgroundColor = value;
          break;
        case "text-decoration":
          reactStyle.textDecoration = value;
          break;
        default:
          reactStyle[key] = value;
      }
    }
    return reactStyle;
  }

  const style: Record<string, string> = {};

  if (token.color) {
    style.color = token.color;
  }

  if (token.bgColor) {
    style.backgroundColor = token.bgColor;
  }

  if (token.fontStyle) {
    if (token.fontStyle & 1) {
      style.fontStyle = "italic";
    }
    if (token.fontStyle & 2) {
      style.fontWeight = "bold";
    }
    if (token.fontStyle & 4) {
      style.textDecoration = `${style.textDecoration || ""} underline`.trim();
    }
  }

  return style;
}
